project(tunnel)
cmake_minimum_required(VERSION 2.8)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

if (COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif (COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else ()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif ()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_TRACE ")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-as-needed")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_TRACE")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lrt")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wreturn-type")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wreturn-type")

if (WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWIN32")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DBIGENDIAN")
else ()
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE")
    #强制引用最新的libstdc++6.0.22以上版本功能，否则在最新的liunx下可能会执行出错
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0 ")
endif ()

#将可执行文件和库文件目录的分别设为build/bin和build/lib,同时清空编译过程的中间文件
set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build)#set as the project directory
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)#.a/.lib
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)#.so/.dll
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)#.exe

include(InstallRequiredSystemLibraries)
#include_directories(${CMAKE_SOURCE_DIR}/include)
# include_directories(${CMAKE_SOURCE_DIR}/src/include)
include_directories(${CMAKE_SOURCE_DIR}/externals/include)
include_directories(${CMAKE_SOURCE_DIR}/externals/include/vtdk)
include_directories(${CMAKE_SOURCE_DIR}/externals/include/ffmpeg)

if (${AARCH64_LINUX})
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/aarch64-linux)
elseif (${ARM64_LINUX})
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/arm64-linux)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/arm64-linux)
elseif (${ANDROID_PHONE})
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/android)
    if (${ARM_V7A})
        message("build armeabi-v7a for android")
        link_directories(${CMAKE_SOURCE_DIR}/externals/lib/arm-android)
    else ()
        message("build arm64-v8a for android")
        link_directories(${CMAKE_SOURCE_DIR}/externals/lib/arm64-android)
    endif ()
elseif (${AARCH64_ANDROID})
    include_directories(${PROJECT_SOURCE_DIR}/tools/toolchain/android-sysroot/include)
    link_directories(${PROJECT_SOURCE_DIR}/tools/toolchain/android-sysroot/lib)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/aarch64-android)
elseif (${ARM_LINUX})
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/arm-linux)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/arm-linux)
elseif (WIN32)
    include_directories(${CMAKE_SOURCE_DIR}/externals/include/x64-win)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/x64-win)
elseif (${X64_LINX26})
    message("BUILD FOR LINX26")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    add_definitions(-DX64_LINX26)
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/x64-linx26)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/x64-linx26)
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/externals/lib/x64-linx26/ DESTINATION .)
elseif (${X64_KYLIN3})
    message("BUILD FOR KYLIN3")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    add_definitions(-DSO_REUSEPORT=15)
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/x64-kylin3)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/x64-kylin3)
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/externals/lib/x64-kylin3/ DESTINATION .)
elseif (${X64_NS42})
    message("BUILD FOR NS42")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    add_definitions(-DSO_REUSEPORT=15)
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/x64-ns42)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/x64-ns42)
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/externals/lib/x64-ns42/ DESTINATION .)
elseif (${KP920_KYLIN})
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/arm64-kp920)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/arm64-kp920)
elseif(${LOONGARCH_UOS})
#    include_directories(${CMAKE_SOURCE_DIR}/externals/include/loongarch-uos)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/loongarch-uos)
else ()
    #include_directories(${CMAKE_SOURCE_DIR}/externals/include/x64-linux)
    link_directories(${CMAKE_SOURCE_DIR}/externals/lib/x64-linux)
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/externals/lib/x64-linux/ DESTINATION .)
endif ()

add_subdirectory(src/srtp_lua)